package lovo01.lesson06;


import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * 发牌器
 */

public class DouDiZhu{

    private ArrayList<PuKe> puKes = new ArrayList<>();

    private List player1;
    private List player2;
    private List player3;
    private List cardHand;
    public DouDiZhu(){
        this.init();
        this.sendCard();
        this.shuffle();
    }
//
    /**
     * 初始化54章扑克牌
     */
    public void init(){
        String[] points = "3,4,5,6,7,8,9,10,J,Q,K,A,2".split(",");
        String[] types = "♠,♥,♣,♦".split(",");
        for(int i = 0;i < points.length;i++){
            for (int j = 0;j < types.length;j++){
                puKes.add(new PuKe(points[i],types[j],i,j));
            }
        }
        puKes.add(new PuKe("大王","",14,0));
        puKes.add(new PuKe("小王","",13,0));

    }
    /**
     * 洗牌
     */
    public void shuffle(){
        Collections.shuffle(puKes);
        System.out.print(puKes);
    }

    /**
     * 发牌
     */
    public void sendCard(){
        player1 = puKes.subList(0,17);
        player2 = puKes.subList(17,34);
        player3 = puKes.subList(34,51);
        cardHand = puKes.subList(51,54);

        sort(player1);
        sort(player2);
        sort(player3);


        System.out.println("玩家一:" + player1);
        System.out.println("玩家二:" + player2);
        System.out.println("玩家三:" + player3);
        System.out.println("地主牌:" + cardHand);

    }

    private void sort(List list){
        Collections.sort(list, new Comparator<PuKe>() {
            @Override
            public int compare(PuKe o1, PuKe o2) {
                if (o1.getPointValue() == o2.getPointValue()){
                    return o1.getTypeValue() - o2.getTypeValue();
                }else {
                    return o2.getPointValue() - o1.getPointValue();
                }
            }
        });
    }

    public List getPlayer1() {
        return player1;
    }

    public void setPlayer1(List player1) {
        this.player1 = player1;
    }

    public List getPlayer2() {
        return player2;
    }

    public void setPlayer2(List player2) {
        this.player2 = player2;
    }

    public List getPlayer3() {
        return player3;
    }

    public void setPlayer3(List player3) {
        this.player3 = player3;
    }

    public List getCardHand() {
        return cardHand;
    }

    public void setCardHand(List cardHand) {
        this.cardHand = cardHand;
    }

    public static void main(String[] args) {

        DouDiZhu douDiZhu = new DouDiZhu();
        douDiZhu.init();
        douDiZhu.shuffle();
        douDiZhu.sendCard();
    }

}
